package com.mall.db.api;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.SmsHomeBrandMapper;
import com.mall.domain.SmsHomeBrand;
import com.mall.interface1.api.SmsHomeBrandServiceApi;
import com.mall.vo.SmsHomePageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsHomeBrandServiceApiImpl implements SmsHomeBrandServiceApi {

    @Autowired
    SmsHomeBrandMapper smsHomeBrandMapper;


    @Override
    public SmsHomePageBeanVo findByPage(String brandName, Integer pageNum, Integer pageSize, Integer recommendStatus) {
        Page<SmsHomeBrand> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsHomeBrand> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(brandName),SmsHomeBrand::getBrandName,brandName);
        qw.eq(recommendStatus != null, SmsHomeBrand::getRecommendStatus, recommendStatus);
        qw.orderByDesc(SmsHomeBrand::getSort);

        Page<SmsHomeBrand> smsHomeBrandPage = smsHomeBrandMapper.selectPage(page, qw);
        return new SmsHomePageBeanVo(pageNum,pageSize,smsHomeBrandPage.getTotal(),smsHomeBrandPage.getRecords());
    }

    @Override
    public void updateRecommendStatus(Long id, Integer recommendStatus) {
        SmsHomeBrand smsHomeBrand = smsHomeBrandMapper.selectById(id);
        smsHomeBrand.setRecommendStatus(recommendStatus);
        smsHomeBrandMapper.updateById(smsHomeBrand);
    }

    @Override
    public void addCreate(SmsHomeBrand smsHomeBrand) {
        smsHomeBrandMapper.insert(smsHomeBrand);
    }

    @Override
    public void delete(Long id) {
        smsHomeBrandMapper.deleteById(id);
    }

    @Override
    public void updateSort(Long id, Integer sort) {
        SmsHomeBrand smsHomeBrand = smsHomeBrandMapper.selectById(id);
        smsHomeBrand.setSort(sort);
        smsHomeBrandMapper.updateById(smsHomeBrand);
    }


}
